package com.bootdo.common.utils;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

public class Token {
	/**** 设置令牌* @param request */
	public static void setToken(HttpServletRequest request) {
		request.getSession().setAttribute("SesToken", UUID.randomUUID().toString());
		System.out.println(request.getSession().getAttribute("SesToken"));
	}

	public static String getToken(HttpServletRequest request) {
		String sessionToken = (String) request.getSession().getAttribute("SesToken");
		if (null == sessionToken || "".equals(sessionToken)) {
			sessionToken = UUID.randomUUID().toString();
			request.getSession().setAttribute("SesToken", sessionToken);
		}
		return sessionToken;
	}

	/**** 验证是否为重复提交* @param HttpServletRequest request* @return String true非重复提交,false重复提交,error非法操作*/
	public static String validToken(HttpServletRequest request){
		String sessionToken = (String)request.getSession().getAttribute("SesToken");
		String requestToken = request.getParameter("SesToken");
		if(null == sessionToken || "null".equals(sessionToken)){sessionToken = "";}
		if(null == requestToken || "null".equals(requestToken) ){requestToken = "";}
		if(sessionToken.equals(requestToken)){
			//返回前一定要重置session中的SesToken
			request.getSession().setAttribute("SesToken",UUID.randomUUID().toString() );
			//非重复提交
			return "true";
			}
		else{
			//返回前一定要重置session中的SesToken
			request.getSession().setAttribute("SesToken",UUID.randomUUID().toString() );
			//重复提交
			return "false";
			}

}}

